<template>
  <!-- 内容 -->
  <div class="main">
    <div class="container pl-5 pr-5 pt-3 pb-3 mt-3 mb-3">
      <h3 class="text-center text-info border-bottom pb-3">登&nbsp;&nbsp;录</h3>
      <form class="mt-5" @submit.prevent="login">
        <div class="form-group row">
          <label for="username" class="col-sm-2 col-form-label text-right">账号:</label>
          <div class="col-sm-10">
            <input type="text" class="form-control is-invalid" id="username" placeholder="请输入您的账号!" required v-model="loginForm.username">
            <div class="invalid-feedback">
              {{ errorMsg.usernameMsg }}
            </div>
          </div>
        </div>
        <div class="form-group row mt-4">
          <label for="password" class="col-sm-2 col-form-label text-right">密码:</label>
          <div class="col-sm-10">
            <input type="password" class="form-control is-invalid" id="password" placeholder="请输入您的密码!" required v-model="loginForm.password">
            <div class="invalid-feedback">
              {{ errorMsg.passwordMsg }}
            </div>
          </div>
        </div>
        <div class="form-group row mt-4">
          <label for="verifycode" class="col-sm-2 col-form-label text-right">验证码:</label>
          <div class="col-sm-6">
            <input type="text" class="form-control is-invalid" id="verifycode" placeholder="请输入验证码!" required v-model="loginForm.code">
            <div class="invalid-feedback">
              {{ errorMsg.codeMsg }}
            </div>
          </div>
          <div class="col-sm-4">
            <img :src="codeImage" style="width:100px;height:40px;" class="mr-2" @click="getCodeImage" />
            <a href="#" class="font-size-12 align-bottom" @click="getCodeImage">刷新验证码</a>
          </div>
        </div>
        <div class="form-group row mt-4">
          <div class="col-sm-2"></div>
          <div class="col-sm-10">
            <input type="checkbox" id="remember-me" v-model="loginForm.rememberMe">
            <label class="form-check-label" for="remember-me">记住我</label>
            <a href="forget" class="text-danger float-right">忘记密码?</a>
          </div>
        </div>
        <div class="form-group row mt-4">
          <div class="col-sm-2"></div>
          <div class="col-sm-10 text-center">
            <button type="submit" class="btn btn-info text-white form-control">立即登录</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import User from '@/api/user'
import router from '@/router'
import { useUserStore } from '@/store/modules/user'
import { setToken } from '../utils/cookie'

const codeImage = ref('')

const loginForm = ref({
  username: '',
  password: '',
  code: '',
  rememberMe: false
})

const errorMsg = ref({
  usernameMsg: '',
  passwordMsg: '',
  codeMsg: '',
})

const userStore = useUserStore()

onMounted(async () => {
  await getCodeImage()
})

const getCodeImage = async () => {
  await User.kaptcha().then(res => {
    codeImage.value = window.URL.createObjectURL(res)
  })
}

const login = async () => {
  await User.login(loginForm.value.username, loginForm.value.password, loginForm.value.code, loginForm.value.rememberMe)
    .then(res => {
      // 保存token
      setToken(res.token)
      // 保存部分用户信息
      res.user.id && (userStore.id = res.user.id)
      res.user.username && (userStore.username = res.user.username)
      res.user.headerUrl && (userStore.headerUrl = res.user.headerUrl)
      res.user.type && (userStore.role = res.user.type)
      // 跳转到首页
      router.replace('/')
    }).catch(res => {
      // 登录失败，显示错误信息
      console.log(res)
      errorMsg.value.usernameMsg = res.usernameMsg ?? ''
      errorMsg.value.passwordMsg = res.passwordMsg ?? ''
      errorMsg.value.codeMsg = res.codeMsg ?? ''
    })
}
</script>

<style scoped>
  @import '@/style/login.css'
</style>
